package com.systemsjr.mpf.db;

import java.util.HashMap;

import com.systemsjr.mpf.expense.Expense;

import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;

public class ExpenseContentProvider extends MpfContentProvider {
	public static final String AUTHORITY = "com.systemsjr.mpf.db.ExpenseContentProvider";
	private static final int EXPENSES = 1;
	private static final int DESCRIPTION = 2;

	protected static UriMatcher sUriMatcher;
	protected static HashMap<String, String> projectionMap;

	@Override
	public boolean onCreate() {
		dbHelper = new MpfDbOpenHelper(this.getContext());
		return true;
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection,
			String[] selectionArgs, String sortOrder) {

		SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
		qb.setTables(Expense.TABLE_NAME);
		System.out.println(sUriMatcher.toString());
		System.out.println(uri.toString());
		
		switch (sUriMatcher.match(uri)) {
		case EXPENSES:
			qb.setProjectionMap(projectionMap);
			break;
		case DESCRIPTION:
			String description = uri.getPathSegments().get(
					Expense.DESCRIPTION_POSITION);

			qb.setProjectionMap(projectionMap);

			if (description != null && description.length() > 0) {
				qb.appendWhere(Expense.DESCRIPTION + " like %" + description
						+ "%");
			}
						
			break;
		default:
			throw new IllegalArgumentException("Unknown URI " + uri);
		}

		SQLiteDatabase db = dbHelper.getReadableDatabase();
		Cursor c = qb.query(db, projection, selection, selectionArgs, null,
				null, sortOrder);
		c.setNotificationUri(getContext().getContentResolver(), uri);

		return c;
	}

	@Override
	public String getType(Uri uri) {

		switch (sUriMatcher.match(uri)) {
		case EXPENSES:
			return Expense.CONTENT_TYPE;
		case DESCRIPTION:
			return Expense.CONTENT_ITEM_TYPE;
		default:
			throw new IllegalArgumentException("Unknown URI " + uri);
		}
	}

	@Override
	public Uri insert(Uri uri, ContentValues values) {

		if (sUriMatcher.match(uri) != EXPENSES) {
			throw new IllegalArgumentException("Unknown URI " + uri);
		}

		ContentValues contentValues;

		if (values != null) {
			contentValues = new ContentValues(values);
		} else {
			contentValues = new ContentValues();
		}

		SQLiteDatabase db = dbHelper.getWritableDatabase();

		long rowid = db.insert(Expense.TABLE_NAME, null, contentValues);

		if (rowid > 0) {
			Uri expenseUri = ContentUris.withAppendedId(Expense.CONTENT_URI,
					rowid);
			getContext().getContentResolver().notifyChange(expenseUri, null);
			return expenseUri;
		}
		throw new SQLException("Failed to insert row into " + uri);
	}

	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs) {

		SQLiteDatabase db = dbHelper.getWritableDatabase();
		int count;

		switch (sUriMatcher.match(uri)) {
		case EXPENSES:
			count = db.delete(Expense.TABLE_NAME, selection, selectionArgs);
			break;
		case DESCRIPTION:
						
			count = db.delete(Expense.TABLE_NAME, selection, selectionArgs);
			break;
		default:
			throw new IllegalArgumentException("Unknown URI " + uri);
		}

		getContext().getContentResolver().notifyChange(uri, null);
		return count;
	}

	@Override
	public int update(Uri uri, ContentValues values, String selection,
			String[] selectionArgs) {

		SQLiteDatabase db = dbHelper.getWritableDatabase();
		int count;

		switch (sUriMatcher.match(uri)) {
		case EXPENSES:
			count = db.update(Expense.TABLE_NAME, values, selection,
					selectionArgs);
			break;
		case DESCRIPTION:
			
			count = db.update(Expense.TABLE_NAME, values, selection,
					selectionArgs);
			break;
		default:
			throw new IllegalArgumentException("Unknown URI " + uri);
		}

		getContext().getContentResolver().notifyChange(uri, null);
		return count;
	}

	static {
		sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
		sUriMatcher.addURI(AUTHORITY, "expense", EXPENSES);
		sUriMatcher.addURI(AUTHORITY, "expense/#", DESCRIPTION);
		// PROJECTION MAP USED FOR ROW ALIAS
		projectionMap = new HashMap<String, String>();
		projectionMap.put(Expense.ID, Expense.ID);
		projectionMap.put(Expense.EXPENSE_TYPE, Expense.EXPENSE_TYPE);
		projectionMap.put(Expense.EXPENSE_DATE, Expense.EXPENSE_DATE);
		projectionMap.put(Expense.AMOUNT, Expense.AMOUNT);
		projectionMap.put(Expense.ACCOUNT_INSTANCE, Expense.ACCOUNT_INSTANCE);
		projectionMap.put(Expense.DESCRIPTION, Expense.DESCRIPTION);
	}

}
